package com.nd.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nd.dao.BrandMapper;
import com.nd.dao.RegionMapper;
import com.nd.pojo.bean.Brand;
import com.nd.pojo.bean.BrandExample;
import com.nd.pojo.bean.Region;
import com.nd.pojo.bean.RegionExample;
import com.nd.pojo.vo.BrandListVO;
import com.nd.pojo.vo.RegionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @author: Aaron
 * @create: 2021-06-04 20:08
 **/
@Service
public class RegionServiceImpl implements RegionService {

    @Autowired
    RegionMapper regionMapper;

    @Override
    public List<RegionVO> regionList() {
        RegionExample regionExample = new RegionExample();
        RegionExample.Criteria criteria = regionExample.createCriteria();
        byte a = 1;
        criteria.andTypeEqualTo(a);
        List<Region> regionProvinceList = regionMapper.selectByExample(regionExample);


        List<RegionVO> regionVOList = new ArrayList<>();

        for (Region province : regionProvinceList) {
            RegionVO regionVO = new RegionVO();
            regionVO.setId(province.getId());
            regionVO.setName(province.getName());
            regionVO.setType(province.getType());
            regionVO.setCode(province.getCode());

            List<RegionVO.ChildrenBeanX> childrenBeanXES = new ArrayList<>();


            RegionExample regionExampleProvince = new RegionExample();
            RegionExample.Criteria criteriaProvince = regionExampleProvince.createCriteria();
            criteriaProvince.andPidEqualTo(province.getId());
            List<Region> regionCityList = regionMapper.selectByExample(regionExampleProvince);


            for (Region city : regionCityList) {
                RegionVO.ChildrenBeanX childrenBeanX = new RegionVO.ChildrenBeanX();
                childrenBeanX.setId(city.getId());
                childrenBeanX.setName(city.getName());
                childrenBeanX.setType(city.getType());
                childrenBeanX.setCode(city.getCode());

                List<RegionVO.ChildrenBeanX.ChildrenBean> childrenBeanList = new ArrayList<>();


                RegionExample regionExampleCity = new RegionExample();
                RegionExample.Criteria criteriaCity = regionExampleCity.createCriteria();
                criteriaCity.andPidEqualTo(city.getId());
                List<Region> regionTown = regionMapper.selectByExample(regionExampleCity);



                for (Region town : regionTown) {
                    RegionVO.ChildrenBeanX.ChildrenBean childrenBean = new RegionVO.ChildrenBeanX.ChildrenBean();
                    childrenBean.setId(town.getId());
                    childrenBean.setName(town.getName());
                    childrenBean.setType(town.getType());
                    childrenBean.setCode(town.getCode());
                    childrenBeanList.add(childrenBean);
                }

                childrenBeanX.setChildren(childrenBeanList);
                childrenBeanXES.add(childrenBeanX);
            }

            regionVO.setChildren(childrenBeanXES);
            regionVOList.add(regionVO);
        }

        return regionVOList;
    }


}
